#include "Avalon/ServiceLocator/ServiceLocatorChannel.hpp"
#include "Avalon/Base/Algorithm.hpp"
#include "Avalon/ServiceLocator/ServiceEntry.hpp"

using namespace Avalon;
using namespace Avalon::IO;
using namespace Avalon::ServiceLocator;
using namespace std;

ServiceLocatorChannel::ServiceLocatorChannel(Channel* channel)
    : m_channel(channel) {}

ServiceLocatorChannel::~ServiceLocatorChannel() {}

const string& ServiceLocatorChannel::GetSessionId() {
  return m_sessionId;
}

void ServiceLocatorChannel::SetSessionId(const string& sessionId) {
  m_sessionId = sessionId;
}

const set<DirectoryEntry::Tag>&
    ServiceLocatorChannel::GetDirectoryEntrySubscriptions() {
  return m_directoryEntrySubscriptions;
}

void ServiceLocatorChannel::AddDirectoryEntrySubscription(
    const DirectoryEntry::Tag& tag) {
  m_directoryEntrySubscriptions.insert(tag);
}

void ServiceLocatorChannel::RemoveDirectoryEntrySubscription(
    const DirectoryEntry::Tag& tag) {
  m_directoryEntrySubscriptions.erase(tag);
}

const vector<string>& ServiceLocatorChannel::GetServiceSubscriptions() {
  return m_serviceSubscriptions;
}

void ServiceLocatorChannel::SubscribeService(const string& serviceName) {
  m_serviceSubscriptions.push_back(serviceName);
}

void ServiceLocatorChannel::UnsubscribeService(const string& serviceName) {
  Remove(m_serviceSubscriptions, serviceName);
}

const vector<ServiceEntry>& ServiceLocatorChannel::GetRegisteredServices() {
  return m_registeredServices;
}

void ServiceLocatorChannel::RegisterService(const ServiceEntry& serviceEntry) {
  m_registeredServices.push_back(serviceEntry);
}

void ServiceLocatorChannel::UnregisterService(int serviceId) {
  for(vector<ServiceEntry>::iterator i = m_registeredServices.begin();
      i != m_registeredServices.end(); ++i) {
    if(i->GetId() == serviceId) {
      m_registeredServices.erase(i);
      return;
    }
  }
}

Connection& ServiceLocatorChannel::GetConnection() {
  return m_channel->GetConnection();
}

Reader& ServiceLocatorChannel::GetReader() {
  return m_channel->GetReader();
}

Writer& ServiceLocatorChannel::GetWriter() {
  return m_channel->GetWriter();
}
